package com.guodu.service.impl;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.guodu.dao.MaterialChukuDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.MaterialChuku;
import com.guodu.entity.MaterialchukuStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.MaterialChukuService;
import com.guodu.vo.MaterialChukuQueryVo;
import com.guodu.vo.MaterialChukuVo;

@Service("materialChukuService")
public class MaterialChukuServiceImpl implements MaterialChukuService {

	@Resource
	private MaterialChukuDao materialChukuDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Override
	public List<MaterialChukuVo> findByVo(MaterialChukuQueryVo materialChukuQueryVo,Units units,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		List<MaterialChuku> list = materialChukuDao.findByVo(materialChukuQueryVo,units, curPage, pageSize);
		List<MaterialChukuVo> materialChukuVos = new ArrayList<MaterialChukuVo>();
		for(MaterialChuku materialChuku : list ){
			MaterialChukuVo materialChukuVo = new MaterialChukuVo(materialChuku);
			UserInfo userInfo = userInfoDao.findById(materialChuku.getCreatePeople());
			Units unitsInfo = unitsDao.findById(userInfo.getUnitsId());
			materialChukuVo.setUnitsName(unitsInfo.getUnitsName());
			materialChukuVo.setIndustry(unitsInfo.getIndustry());
			materialChukuVo.setUnitsId(unitsInfo.getUnitsId());
			if(unitsInfo.getFunitsId()!=null){
				Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
				materialChukuVo.setFunitsName(funitsInfo.getUnitsName());
				materialChukuVo.setFunitsId(funitsInfo.getUnitsId());
			}
			materialChukuVo.setUserName(userInfo.getUserName());
			materialChukuVos.add(materialChukuVo);
		}
		return materialChukuVos;
	}

	@Override
	public int findSizeByVo(MaterialChukuQueryVo materialChukuQueryVo,Units units) {
		// TODO Auto-generated method stub
		return materialChukuDao.findSizeByVo(materialChukuQueryVo,units);
	}

	@Override
	public MaterialChuku findById(String id) {
		// TODO Auto-generated method stub
		return materialChukuDao.findById(id);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		materialChukuDao.del(id);
	}

	@Override
	public void delByIds(String[] ids) {
		// TODO Auto-generated method stub
		materialChukuDao.delByIds(ids);
	}

	@Override
	public void save(MaterialChuku entity) {
		// TODO Auto-generated method stub
		materialChukuDao.save(entity);
	}

	@Override
	public void examine(String ids,String reason,String beforeStatus,Long afterStatus,UserInfo userInfo) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<MaterialchukuStatus> entities = new ArrayList<MaterialchukuStatus>();
		for(int i=0;i<idses.length;i++){
			MaterialchukuStatus entity = new MaterialchukuStatus();
			entity.setAfterStatus(afterStatus);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(userInfo.getUserId());
			entity.setMaterialId(idses[i]);
			entity.setReason(reason);
			entity.setCreateTime(Calendar.getInstance().getTime());
			entity.setStatusId(i+"");
			entities.add(entity);
		}
		materialChukuDao.changeStatus(entities);
		materialChukuDao.examine(ids,afterStatus);
	}

	@Override
	public void update(MaterialChuku entity) {
		// TODO Auto-generated method stub
		materialChukuDao.update(entity);
	}

	@Override
	public List<MaterialchukuStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return materialChukuDao.findStatus(id);
	}

	@Override
	public List<MaterialChuku> findAllByVo(
			MaterialChukuQueryVo materialChukuQueryVo, Units units) {
		// TODO Auto-generated method stub
		return materialChukuDao.findAllByVo(materialChukuQueryVo, units);
	}

	@Override
	public BigDecimal findAllMoney(MaterialChukuQueryVo materialChukuQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialChukuDao.findAllMoney(materialChukuQueryVo, units);
	}

	@Override
	public boolean examinAll(MaterialChukuQueryVo materialChukuQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialChukuDao.examinAll(materialChukuQueryVo, units);
	}
	@Override
	public boolean huituiAll(MaterialChukuQueryVo materialChukuQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialChukuDao.huituiAll(materialChukuQueryVo, units);
	}

	@Override
	public boolean allBack(MaterialChukuQueryVo materialChukuQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialChukuDao.allBack(materialChukuQueryVo,units);
	}

	@Override
	public boolean shanchuAll(MaterialChukuQueryVo materialChukuQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return materialChukuDao.shanchuAll(materialChukuQueryVo,units);
	}

	



}
